package com.ygqh.baby.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.GroupMode;
import com.ygqh.baby.ao.PromotionStatus;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.constant.RedisConstant;
import com.ygqh.baby.mapper.YgTuanMapper;
import com.ygqh.baby.po.YgTuan;
import com.ygqh.baby.po.YgTuanExample;
import com.ygqh.baby.redis.RedisDao;
import com.ygqh.baby.service.YgTuanService;
import com.ygqh.baby.utils.SerializeUtils;
import com.ygqh.baby.utils.TuanAttributeComparator;

@Service
public class YgTuanServiceImpl implements YgTuanService {
	
	@Autowired
	private YgTuanMapper ygTuanMapper;
	@Autowired
	private RedisDao redisDao;
	@Autowired
	private TuanAttributeComparator tuanAttributeComparator;

	@Override
	public List<YgTuan> find() {
		YgTuanExample example = new YgTuanExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygTuanMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgTuanExample example = new YgTuanExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygTuanMapper.countByExample(example);
	}

	@Override
	public YgTuan findById(Long id) {
		return ygTuanMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgTuan ygTuan) {
		return ygTuanMapper.insertSelective(ygTuan);
	}

	@Override
	public int update(YgTuan ygTuan) {
		return ygTuanMapper.updateByPrimaryKeySelective(ygTuan);
	}

	@Override
	public ResultSet<YgTuan> search(QueryInfo queryInfo, String q, PromotionStatus pstatus, Date startDate, Date endDate) {
		List<YgTuan> list = ygTuanMapper.selectSuper(queryInfo, q, pstatus == null ? null : pstatus.name(), startDate, endDate);
		int count = ygTuanMapper.countSuper(q, pstatus == null ? null : pstatus.name(), startDate, endDate);
		ResultSet<YgTuan> resultSet = new ResultSet<YgTuan>(count, list);
		return resultSet;

	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgTuanExample example = new YgTuanExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgTuan record = new YgTuan();
		record.setStatus(DataStatus.Delete);

		return ygTuanMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[] { id });
	}

	@Override
	public int saveOrUpdate(YgTuan ygTuan) {
		int r = 0;
		if (ygTuan.getId() != null) {
			r = this.update(ygTuan);
		} else {
			r = this.save(ygTuan);
		}
		setTuanAttributeToRedis();
		return r;
	}

	private void setTuanAttributeToRedis() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				findTuanAttributeFromDb();
			}
		}).start();
	}

	@Override
	public List<YgTuan> findList(QueryInfo queryInfo, PromotionStatus pstatus, Date startDate, Date endDate, GroupMode groupMode, String attribute) {
		return ygTuanMapper.selectList(queryInfo, pstatus == null ? null : pstatus.name(), startDate, endDate, groupMode, attribute);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<String> findTuanAttribute() {
		byte[] bs = redisDao.get(RedisConstant.TUAN_ATTRIBUTE.getBytes());
		if (!SerializeUtils.isEmpty(bs)) {
			return (List<String>) SerializeUtils.deserialize(bs);
		}
		return findTuanAttributeFromDb();
	}
	
	private List<String> findTuanAttributeFromDb() {
		List<String> resultList = new ArrayList<>();
		List<String> attributeList = ygTuanMapper.selectAllAttribute();
		if (CollectionUtils.isEmpty(attributeList)) {
			return resultList;
		}
		for (String attribute : attributeList) {
			if (StringUtils.isBlank(attribute)) {
				continue;
			}
			String string = attribute.replaceAll("拼团-", "");
			if (attribute.indexOf(",") != -1) {
				pushItemToArray(string, resultList);
			} else if (resultList.indexOf(string) == -1) {
				resultList.add(string);
			}
		}
		resultList.sort(tuanAttributeComparator);
		redisDao.set(RedisConstant.TUAN_ATTRIBUTE.getBytes(), SerializeUtils.serialize(resultList));
		return resultList;
	}

	private static void pushItemToArray(String attribute, List<String> attributeList) {
		String[] split = attribute.split(",");
		for (String string : split) {
			if (attributeList.indexOf(string) == -1) {
				attributeList.add(string);
			}
		}
	}

}
